package com.css.zfzx.fxpg.modules.picture.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.modules.picture.repository.entity.FxPictureEntity;
import com.css.zfzx.fxpg.modules.picture.service.FxPictureService;
import com.css.zfzx.fxpg.modules.picture.viewobjects.FxPictureQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-17
 */
@Slf4j
@RestController
@RequestMapping("fx/fxPictures")
public class FxPictureController {
    @Autowired
    private FxPictureService fxPictureService;

    @GetMapping("/queryFxPictures")
    public RestResponse queryFxPictures(HttpServletRequest request, FxPictureQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxPictureService.queryFxPictures(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxPicture(@PathVariable String id) {
        RestResponse response = null;
        try{
            FxPictureEntity fxPicture = fxPictureService.getFxPicture(id);
            response = RestResponse.succeed(fxPicture);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveFxPicture(@RequestBody FxPictureEntity fxPicture) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPictureService.saveFxPicture(fxPicture);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateFxPicture(@RequestBody FxPictureEntity fxPicture)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPictureService.updateFxPicture(fxPicture);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @DeleteMapping
    public RestResponse deleteFxPictures(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            fxPictureService.deleteFxPictures(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file) {
        RestResponse restResponse = null;
        try {
            String s = fxPictureService.importExcel(file);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导入失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

}